<?php
namespace Home\Controller;
use Think\Controller;
class AliPayController extends Controller {

  //   public function pay(){

  //       $order_name = I('post.order_name');
  //       $order_fee = I('post.order_fee');
  //       $order_desc = '支付接口测试';


  //       //订单号
  //       $sn = date('YmdHis',time());


  //       //支付交易请求
		// D('pay_record')->buildRequestForm($sn,$order_fee,1,12658,$order_name,$order_desc);

  //   }


    /**
     * 业务处理
     *
     * @return void
     * @author
     **/
    private function handle($out_trade_no,$total_fee,$trade_status,
    						$buyer_email,$trade_no,$notify_id,$notify_time)
    {
    	//支付通知验证
    	$checkstatus = D('Pay')->payCheck($trade_status);

    	if($checkstatus){
    		//记录支付宝支付记录信息
            $data['out_trade_no'] = $out_trade_no;
            $data['total_fee'] = $total_fee;
            $data['trade_no'] = $trade_no;
            $data['buyer_email'] = $buyer_email;
            $data['notify_time'] = $notify_time;
            $data['trade_status'] = $trade_status;

            D('alipay_list')->add($data);

            //根据支付宝返回的订单号查询对应订单号的在线支付记录
            $record = D('pay_record')->where('order_sn='.$out_trade_no)->find();

            //判断防止重复操作
	    	if(!$record['status']){

	    		//把对应订单号的支付宝充值记录修改成支付成功状态
	    		D('pay_record')->setField('status',1);

	    		//根据类型做相应操作
	    		switch ($record['type']) {
	    			case 1:
	    				//会员充值成功

	    				break;
					case 2:
						// 商品首付成功
						break;
					case 3:
						//商品全款购买成功
						break;
					case 3:
						//钱包充值成功
						break;
	    			default:

	    				break;
	    		}

	    		return $record;
	    	}

    	}else{
    		//验证失败
    		return false;
    	}





    }

    /**
     * 支付宝处理结果同步回调
     *
     * @return void
     * @author
     **/
    public function alipayReturn()
    {
    	//获取支付宝同步返回的数据
    	$notify_time = I('get.notify_time');//通知时间
    	$trade_no = I('get.trade_no');//支付宝交易号
    	$notify_id = I('get.notify_id');//通知校验ID
    	$out_trade_no = I('get.out_trade_no');//订单号
    	$trade_status = I('get.trade_status');//交易状态
    	$buyer_email = I('get.buyer_email');//买家支付宝账号
        $total_fee = I('get.total_fee');//订单金额


    	$record =  $this->handle(
    		$out_trade_no,
    		$total_fee,
    		$trade_status,
    		$buyer_email,
    		$trade_no,
    		$notify_id,
    		$notify_time
    		);

    	//根据类型进行页面跳转
    	if($record){
    		switch ($record['type']) {
    			case 1:
    				 $this->success('会员充值成功');
    				break;
				case 2:
					// 商品首付成功
					$this->success('商品首付成功');
					break;
				case 3:
					//商品全款购买成功
					$this->success('商品全款购买成功');
					break;
				case 3:
					//钱包充值成功
					$this->success('钱包充值成功');
					break;
    			default:

    				break;
    		}
    	}else{
    		$this->error('非法操作');
    	}


    }

    /**
     * 支付宝处理结果异步回调
     *
     * @return void
     * @author
     **/
    public function alipayNotify()
    {
    	//获取支付宝异步返回的数据
    	$notify_time = I('post.notify_time');//通知时间
    	$trade_no = I('post.trade_no');//支付宝交易号
    	$notify_id = I('post.notify_id');//通知校验ID
    	$out_trade_no = I('post.out_trade_no');//订单号
    	$trade_status = I('post.trade_status');//交易状态
    	$buyer_email = I('post.buyer_email');//买家支付宝账号
    	$total_fee = I('post.total_fee');//订单金额

    	$record = $this->handle(
    		$out_trade_no,
    		$total_fee,
    		$trade_status,
    		$buyer_email,
    		$trade_no,
    		$notify_id,
    		$notify_time
    		);

    	echo  !empty($record)?'success':'error';die;

    }

}